package com.wyj.config;

import com.wyj.api.UserApi;
import com.wyj.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

@Component
@Slf4j
public class MyCommandLineRunner implements CommandLineRunner {
    @DubboReference(interfaceClass = UserApi.class, version = "${dubbo.service.user.version}")
    private UserApi userApi;
    AtomicInteger a = new AtomicInteger(0);
    AtomicInteger b = new AtomicInteger(0);
    AtomicInteger c = new AtomicInteger(0);


    final ExecutorService executorService1 = Executors.newFixedThreadPool(2);
    final ExecutorService executorService2 = Executors.newFixedThreadPool(2);
    final ExecutorService executorService3 = Executors.newFixedThreadPool(2);


    @Override
    public void run(String... args) throws Exception {
        var now = LocalDateTime.now();
        final var plus = now.plus(1, ChronoUnit.MINUTES);
        while (plus.isAfter(now)) {
            executorService1.submit(() -> {
                final var user = userApi.methodA();
                a.incrementAndGet();
                log.info("userApi.methodA={}", user);
            });
            executorService2.submit(() -> {
                final var user = userApi.methodB();
                b.incrementAndGet();
                log.info("userApi.methodB={}", user);
            });
            executorService3.submit(() -> {
                final var user = userApi.methodC();
                c.incrementAndGet();
                log.info("userApi.methodC={}", user);
            });
            Thread.sleep(50);
            now = LocalDateTime.now();
        }
        System.err.println(a.get() + "  " + b.get() + "   " + c.get());
    }
}
